<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="../resources/user-gesture-utils.js"></script>
<script src="vibration-utils.js"></script>
</head>
<body>
<h4 id="test"></h4>
<script>
description('Tests for how durations are handled in the Vibration API.');

if (window.testRunner) {
  testRunner.dumpAsText();
  testRunner.dumpChildFrames();
}

// Simulates a user click for vibrate to be allowed.
var element = document.getElementById("test");
simulateUserClick(element.offsetLeft + 2, element.offsetTop + 2);

// Empty pattern is empty. Does not start a vibration.
shouldBeTrue("navigator.vibrate([])");
shouldBeTrue("areArraysEqual(internals.pendingVibrationPattern(navigator), [])");
shouldBeFalse("internals.isVibrating(navigator)");
stopVibration();

// An empty pattern is stored internally for a single 0.
shouldBeTrue("navigator.vibrate(0)");
shouldBeTrue("areArraysEqual(internals.pendingVibrationPattern(navigator), [])");
shouldBeFalse("internals.isVibrating(navigator)");
stopVibration();

// An empty pattern is stored internally for a single 0.
shouldBeTrue("navigator.vibrate([0])");
shouldBeTrue("areArraysEqual(internals.pendingVibrationPattern(navigator), [])");
shouldBeFalse("internals.isVibrating(navigator)");
stopVibration();

// The maximum vibration duration.
shouldBeTrue("navigator.vibrate(10000)");
shouldBeTrue("areArraysEqual(internals.pendingVibrationPattern(navigator), [10000])");
shouldBeTrue("internals.isVibrating(navigator)");
stopVibration();

// Overly long vibrations are truncated to the maximum duration.
shouldBeTrue("navigator.vibrate(10001)");
shouldBeTrue("areArraysEqual(internals.pendingVibrationPattern(navigator), [10000])");
shouldBeTrue("internals.isVibrating(navigator)");
stopVibration();

// All values in the sequence are truncated.
shouldBeTrue("navigator.vibrate([10001, 10001, 10001])");
shouldBeTrue("areArraysEqual(internals.pendingVibrationPattern(navigator), [10000, 10000, 10000])");
shouldBeTrue("internals.isVibrating(navigator)");
stopVibration();

// A negative integer is clamped to 0. This becomes an empty pattern.
shouldBeTrue("navigator.vibrate(-1)");
shouldBeTrue("areArraysEqual(internals.pendingVibrationPattern(navigator), [])");
shouldBeFalse("internals.isVibrating(navigator)");
stopVibration();

// WebIDL cannot clamp values in sequences yet, so -1 first overflows to a huge
// number, then it is truncated to the maximum duration.
// All values in the sequence overflow and truncate in the same way.
// This is a bug, and this test exposes the incorrect behavior.
// FIXME: http://crbug.com/309700
shouldBeTrue("navigator.vibrate([-1, -1, -1])");
shouldBeTrue("areArraysEqual(internals.pendingVibrationPattern(navigator), [10000, 10000, 10000])");
shouldBeTrue("internals.isVibrating(navigator)");
stopVibration();

</script>
</body>
</html>
